package com.wandong.cinema.entity;

import com.wandong.cinema.service.MovieService;
import com.wandong.cinema.service.ShowService;
import com.wandong.cinema.service.TicketService;
import com.wandong.cinema.service.impl.MovieServiceImpl;
import com.wandong.cinema.service.impl.ShowServiceImpl;
import com.wandong.cinema.service.impl.TicketServiceImpl;
import com.wandong.cinema.util.InputUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Receptionist extends User {
    private final ShowService showService = new ShowServiceImpl();
    private final TicketService ticketService = new TicketServiceImpl(showService);

    public Receptionist(String userId, String username, String password, Date registerTime, String userType, String phone) {
        super(userId, username, password, registerTime, userType, phone);
    }

    @Override
    public void showMenu() {
        while (true) {
            System.out.println("\n=== 前台菜单 ===");
            System.out.println("1. 售票");
            System.out.println("2. 退票");
            System.out.println("3. 查询场次");
            System.out.println("4. 查询影片");
            System.out.println("5. 修改个人信息");
            System.out.println("6. 退出登录");
            System.out.print("请选择操作: ");
            
            int choice = InputUtil.inputInteger();
            switch (choice) {
                case 1:
                    sellTicket();
                    break;
                case 2:
                    refundTicket();
                    break;
                case 3:
                    showService.queryShows();
                    break;
                case 4:
                    queryMovies();
                    break;
                case 5:
                    updatePersonalInfo();
                    break;
                case 6:
                    System.out.println("退出登录成功");
                    return;
                default:
                    System.out.println("无效选择，请重新输入");
            }
        }
    }

    private void sellTicket() {
        try {
            System.out.println("\n=== 售票 ===");
            System.out.print("请输入场次ID: ");
            String showId = InputUtil.inputString();

            // 使用新的交互式售票方法
            ((TicketServiceImpl) ticketService).interactiveSellTicket(showId);
        } catch (Exception e) {
            System.out.println("售票失败: " + e.getMessage());
        }
    }

    private void refundTicket() {
        try {
            System.out.println("\n=== 退票 ===");
            System.out.print("请输入票号: ");
            String ticketId = InputUtil.inputString();
            
            boolean success = ticketService.refundTicket(ticketId);
            if (success) {
                System.out.println("退票成功");
            }
        } catch (Exception e) {
            System.out.println("退票失败: " + e.getMessage());
        }
    }

    private void queryMovies() {
        System.out.println("\n=== 影片查询 ===");
        System.out.print("请输入影片名称(空为查询全部): ");
        String name = InputUtil.inputString();
        List<Movie> result = new ArrayList<>();
        MovieService movieService = new MovieServiceImpl();
        if (name.isEmpty()) {
            result = movieService.getAllMovies();
        } else {
            for (Movie movie : movieService.getAllMovies()) {
                if (movie.getName().contains(name)) {
                    result.add(movie);
                }
            }
        }

        if (result.isEmpty()) {
            System.out.println("没有找到符合条件的影片");
            return;
        }

        System.out.println("\n查询结果:");
        for (Movie movie : result) {
            System.out.println(movie);
        }
    }

    private void updatePersonalInfo() {
        System.out.println("\n=== 修改个人信息 ===");
        System.out.print("请输入新密码: ");
        String newPassword = InputUtil.inputString();
        System.out.print("请输入新手机号: ");
        String newPhone = InputUtil.inputString();
        
        setPassword(newPassword);
        setPhone(newPhone);
        System.out.println("个人信息修改成功");
    }

}
